;
$(function() {
	var t = '<div>\
				<small>\
					<table class="table margin-0" width="100%">\
						<tr>\
							<td class="text-center">一级编码</td>\
							<td><input type="text" name="code1"/></td>\
						</tr>\
						<tr>\
							<td class="text-center">二级编码</td>\
							<td><input type="text" name="code2"/></td>\
						</tr>\
						<tr>\
							<td class="text-center">三级编码</td>\
							<td><input type="text" name="code3"/></td>\
						</tr>\
						<tr>\
							<td class="text-center">四级编码</td>\
							<td><input type="text" name="code4"/></td>\
						</tr>\
						<tr>\
							<td colspan="2" class="text-center">\
								<div class="btn btn-primary btn-sm pop-ok">确定</div>\
								&nbsp;\
								&nbsp;\
								<div class="btn btn-danger btn-sm pop-cancel">取消</div>\
							</td>\
						</tr>\
					</table>\
				</small>\
			</div>';

	$('.encode-no').on('click', function() {
		$(this).popover({
			delay : {
				"show" : 50,
				"hide" : 100
			},
			html : true,
			trigger : 'manual',
			placement : 'bottom',
			template : '<div class="popover" role="tooltip" style="min-width: 300px; max-width: 300px;"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content padding-0"></div></div>',
			content : function() {
				return t;
			}
		});

		$(this).popover('show');
	});

	$('.filter-form').on('keyup', '[name="code1"]', function(event) {
		var $this = $(this);

		if ($this.val().length > 1)
			$this.val($this.val().substring(0, 1));
	});

	$('.filter-form').on('keyup', '[name="code2"]', function(event) {
		var $this = $(this);

		if ($this.val().length > 1)
			$this.val($this.val().substring(0, 1));
	});

	$('.filter-form').on('keyup', '[name="code3"]', function(event) {
		var $this = $(this);

		if ($this.val().length > 4)
			$this.val($this.val().substring(0, 4));
	});

	$('.filter-form').on('keyup', '[name="code4"]', function(event) {
		var $this = $(this);

		if ($this.val().length > 3)
			$this.val($this.val().substring(0, 3));
	});

	$('.filter-form').on('click', '.pop-ok', function() {
		var ret = '';

		var c1 = $('[name="code1"]');
		if (!c1.val())
			ret += '_';
		else
			ret += c1.val();

		var c2 = $('[name="code2"]');
		if (!c2.val())
			ret += '_';
		else
			ret += c2.val();
		ret += '-';

		var c3 = $('[name="code3"]');
		if (!c3.val())
			ret += '____';
		else
			ret += c3.val();
		ret += '-';

		var c4 = $('[name="code4"]');
		if (!c4.val())
			ret += '__';
		else
			ret += c4.val();

		$('.encode-no').val(ret);

		$('.encode-no').popover('destroy');
	});

	$('.filter-form').on('click', '.pop-cancel', function() {
		$('.encode-no').popover('destroy');
	});
});